<template>
  <div class="equipment-dashboard">
    <div class="dashboard-header">
      <h2>设备监控面板</h2>
    </div>
    
    <div class="dashboard-content">
      <div class="control-panel">
        <div class="control-group">
          <h3>设备控制</h3>
          <div class="control-item">
            <el-button type="primary" @click="highlightEquipment">高亮显示设备</el-button>
          </div>
          <div class="control-item">
            <el-button type="success" @click="addEquipment">添加设备</el-button>
          </div>
          <div class="control-item">
            <el-button type="warning" @click="removeEquipment">移除设备</el-button>
          </div>
        </div>
        
        <div class="control-group">
          <h3>视角控制</h3>
          <div class="control-item">
            <el-button @click="viewEquipmentArea">查看设备区域</el-button>
          </div>
        </div>
      </div>
      
      <div class="info-panel">
        <div class="info-card">
          <h3>设备统计</h3>
          <p>设备总数: {{ equipmentCount }}</p>
          <p>运行中设备: {{ runningEquipment }}</p>
          <p>故障设备: {{ faultyEquipment }}</p>
        </div>
        
        <div class="info-card">
          <h3>实时状态</h3>
          <div class="status-item" v-for="item in equipmentStatus" :key="item.id">
            <span>{{ item.name }}</span>
            <el-tag :type="item.status === '正常' ? 'success' : item.status === '警告' ? 'warning' : 'danger'">
              {{ item.status }}
            </el-tag>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import * as THREE from 'three'

// 接收来自父组件的 threeJSViewer 实例
const props = defineProps({
  threeJSViewer: {
    type: Object,
    required: true
  }
})

// 设备数据
const equipmentCount = ref(12)
const runningEquipment = ref(10)
const faultyEquipment = ref(2)

const equipmentStatus = ref([
  { id: 1, name: '空调系统-01', status: '正常' },
  { id: 2, name: '电梯系统-01', status: '正常' },
  { id: 3, name: '消防系统-01', status: '警告' },
  { id: 4, name: '电力系统-01', status: '正常' },
  { id: 5, name: '给排水系统-01', status: '故障' }
])

// 方法定义
function highlightEquipment() {
  // 在3D场景中高亮显示设备的示例方法
  console.log('高亮显示设备')
}

function addEquipment() {
  // 添加设备到3D场景
  const geometry = new THREE.SphereGeometry(0.5, 16, 16)
  const material = new THREE.MeshPhongMaterial({ 
    color: 0xff0000,
    emissive: 0x220000,
    shininess: 30 
  })
  const equipment = new THREE.Mesh(geometry, material)
  
  // 随机放置设备位置
  equipment.position.set(
    (Math.random() - 0.5) * 8,
    2,
    (Math.random() - 0.5) * 8
  )
  
  props.threeJSViewer.addModel(equipment)
  equipmentCount.value++
  runningEquipment.value++
}

function removeEquipment() {
  const scene = props.threeJSViewer.getScene()
  if (scene && scene.children.length > 2) { // 保留默认立方体和光源
    const lastObject = scene.children[scene.children.length - 1]
    if (lastObject.isMesh) {
      scene.remove(lastObject)
      equipmentCount.value--
      if (lastObject.material.color.getHex() === 0xff0000) {
        runningEquipment.value--
      }
    }
  }
}

function viewEquipmentArea() {
  props.threeJSViewer.setCameraPosition({ x: 5, y: 5, z: 5 })
}
</script>

<style scoped>
.equipment-dashboard {
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  margin: 20px;
  backdrop-filter: blur(10px);
  height: calc(100% - 40px);
  overflow-y: auto;
}

.dashboard-header {
  border-bottom: 1px solid #eee;
  padding-bottom: 15px;
  margin-bottom: 20px;
}

.dashboard-header h2 {
  margin: 0;
  color: #333;
}

.dashboard-content {
  display: flex;
  gap: 20px;
  height: calc(100% - 80px);
}

.control-panel {
  flex: 1;
  background: #f5f5f5;
  padding: 15px;
  border-radius: 6px;
}

.info-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.control-group {
  margin-bottom: 20px;
}

.control-group h3 {
  margin-top: 0;
  color: #555;
}

.control-item {
  margin-bottom: 10px;
}

.info-card {
  background: #f5f5f5;
  padding: 15px;
  border-radius: 6px;
}

.info-card h3 {
  margin-top: 0;
  color: #555;
}

.info-card p {
  margin: 8px 0;
  color: #666;
}

.status-item {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}

.status-item:last-child {
  border-bottom: none;
}
</style>